<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <h1>买买买</h1>
    <h1>买买买</h1>
    <h1>买买买</h1>
    <h1>买买买</h1>
    <h1>买买买</h1>
    <h1>买买买</h1>
    <h1>买买买</h1>
    <h1>买买买</h1>
    <h1>买买买</h1>
    <h1>买买买</h1>
    <h1>买买买</h1>
    <h1>买买买</h1>
    <h1>买买买</h1>
    <h1>买买买</h1>
    <img
      data-src="https://tse1-mm.cn.bing.net/th/id/OIP-C.R78qgeMyzQsqHRhqZc0-UAHaOH?w=181&h=345&c=7&r=0&o=5&pid=1.7"
      alt=""
    />
    <h1>买买买</h1>
    <h1>买买买</h1>
    <h1>买买买</h1>
    <h1>买买买</h1>
    <h1>买买买</h1>
    <h1>买买买</h1>
    <h1>买买买</h1>
    <h1>买买买</h1>
    <h1>买买买</h1>
    <h1>买买买</h1>
    <h1>买买买</h1>
    <h1>买买买</h1>
    <h1>买买买</h1>
    <h1>买买买</h1>
    <h1>买买买</h1>
    <h1>买买买</h1>
    <h1>买买买</h1>
    <h1>买买买</h1>
    <h1>买买买</h1>
    <h1>买买买</h1>
    <h1>买买买</h1>
    <h1>买买买</h1>
    <img
      data-src="https://tse1-mm.cn.bing.net/th/id/OIP-C.-hymx1fg76CijuTLrNJKhwHaEE?w=305&h=180&c=7&r=0&o=5&pid=1.7"
      alt=""
    />
    <h1>买买买</h1>
    <h1>买买买</h1>
    <h1>买买买</h1>
    <h1>买买买</h1>
    <h1>买买买</h1>
    <h1>买买买</h1>
    <h1>买买买</h1>
    <h1>买买买</h1>
    <img
      data-src="https://tse3-mm.cn.bing.net/th/id/OIP-C.t9EgVdwDMPPwvoC47_AmYAHaJY?w=196&h=248&c=7&r=0&o=5&pid=1.7"
      alt=""
    />
    <h1>买买买</h1>
    <h1>买买买</h1>
    <h1>买买买</h1>
    <h1>买买买</h1>
    <h1>买买买</h1>
    <h1>买买买</h1>
    <h1>买买买</h1>
    <h1>买买买</h1>
    <img
      data-src="https://tse2-mm.cn.bing.net/th/id/OIP-C.kmR2P7xa78Rq6v20OWTCWwAAAA?w=115&h=180&c=7&r=0&o=5&pid=1.7"
      alt=""
    />
    <h1>买买买</h1>
    <script>
      var imgName = document.getElementsByTagName('img');
      // 图片懒操作，等图片出现在视口中加载，再把data-src换成src
      document.addEventListener('scroll', function () {
        var viewTop = window.scrollY;
        //循环所有图片
        for (var i = 0; i < imgName.length; i++) {
            //如果图片已加载，就跳过此次执行
          if (imgName[i].getAttribute('loaded')) {
            continue;
          }
          var area = imgName[i].getBoundingClientRect().top;
          //当图片视口高度小于视口高度，把data-src 换成src
          if (area < viewTop) {
            imgName[i].src = imgName[i].getAttribute('data-src');
            //给已加载的图片做个标记
            imgName[i].setAttribute('loaded', true);
          }
        }
      });
    </script>
  </body>
</html>
